package com.mrd.gtimp.base.action;

import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.Address;
import com.mrd.gtimp.base.service.AddressBiz;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author yangyl
 */
@Controller
@RequestMapping("/base")
public class AddressAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private AddressBiz addressBiz;

    /**
     * 返回前台的结果变量，格式为[[显示值，真实值],[显示值1，真实值1],...]
     */
    private String[][] results;

    @RequestMapping("/address-dropdown")
    @ResponseBody
    public String dropdown(Address address, String areaListJson, String k) {
        List<Address> addresses = null;
        String code = HttpUtils.getParam("code");
        // 区域等级
        String layer = HttpUtils.getParam("layer");
        // 查询关键字
        String keyword = HttpUtils.getParam("keyword");

        areaListJson = "";
        if (keyword != null) {
            // keyword = new String(keyword.getBytes("ISO-8859-1"), "UTF-8");
            try {
                keyword = URLDecoder.decode(keyword, "UTF-8");
                String sql = "select t.code, t.name, t.layer, t.parent from gen_area t ";
                if (layer != null && layer.equalsIgnoreCase("3")) {
                    sql = " select t.code, t.name, t.layer, t.parent, a.name as aname from gen_area t left join gen_area a on t.parent=a.code ";
                } else if (layer != null && layer.equalsIgnoreCase("4")) {
                    sql = " select t.code, t.name, t.layer, t.parent, a.name as aname, b.name as bname from gen_area t left join gen_area a on t.parent=a.code left join gen_area b on a.parent=b.code ";
                }
                sql += " where t.layer='" + layer + "' ";
                if (keyword != null && !keyword.equals("")) {
                    sql += " and t.name like '" + keyword + "%' ";
                } else {
                    if (!layer.equalsIgnoreCase("2") && (code == null || code.equals(""))) {
                        return null;
                    }
                }
                if (code != null && !code.equals("")) {
                    sql += " and t.parent='" + code + "' ";
                }
                sql += " order by t.sort ";
                List<Object> objects = addressBiz.findObjectBySql(sql, 1, 50, null);
                if (addresses == null) {
                    addresses = new ArrayList<Address>();
                }
                if (objects != null && objects.size() > 0) {
                    // areaListJson = "{\"areaListJson\":[";
                    for (Object b : objects) {
                        Object[] bb = (Object[]) b;
                        address = new Address();
                        address.setCode(bb[0].toString());
                        address.setName(bb[1].toString());
                        String parentName = "";
                        if (layer != null && layer.equalsIgnoreCase("3")) {
                            parentName += bb[4].toString();
                        } else if (layer != null && layer.equalsIgnoreCase("4")) {
                            parentName += bb[4].toString() + "," + bb[5].toString();
                        }
                        address.setParentName(parentName);
                        // areaListJson += "{\"code\":\"" + bb[0].toString() + "\",\"name\":\""
                        // + bb[1].toString() + "\",\"parentName\":\"" + parentName + "\"},";
                        addresses.add(address);
                    }
                    // if(areaListJson.endsWith(",")) {
                    // areaListJson = areaListJson.substring(0, areaListJson.length() - 1);
                    // }
                    // areaListJson += "]}";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (keyword == null) {
            if (k != null) {
                try {
                    String p = HttpUtils.getParam("p");
                    String c = HttpUtils.getParam("c");
                    if (p != null && !"".equals(p)) {
                        code = p;
                    }
                    if (c != null && !"".equals(c)) {
                        code = c;
                    }
                    k = URLDecoder.decode(k, "UTF-8");
                    String sql = "select t.code, t.name, t.layer, t.parent from gen_area t ";
                    if (layer != null && layer.equalsIgnoreCase("3")) {
                        sql = " select t.code, t.name, t.layer, t.parent, a.name as aname from gen_area t left join gen_area a on t.parent=a.code ";
                    } else if (layer != null && layer.equalsIgnoreCase("4")) {
                        sql = " select t.code, t.name, t.layer, t.parent, a.name as aname, b.name as bname from gen_area t left join gen_area a on t.parent=a.code left join gen_area b on a.parent=b.code ";
                    }
                    sql += " where t.layer='" + layer + "' ";
                    if (k != null && !k.equals("")) {
                        sql += " and t.name like '" + k + "%' ";
                    } else {
                        if (!layer.equalsIgnoreCase("2") && (code == null || code.equals(""))) {
                            return null;
                        }
                    }
                    if (code != null && !code.equals("")) {
                        sql += " and t.parent='" + code + "' ";
                    }
                    List<Object> objects = addressBiz.findObjectBySql(sql, 1, 50, null);
                    addresses = new ArrayList<Address>();
                    if (objects != null && objects.size() > 0) {
                        for (Object b : objects) {
                            Object[] bb = (Object[]) b;
                            address = new Address();
                            address.setCode(bb[0].toString());
                            address.setName(bb[1].toString());
                            String parentName = "";
                            if (layer != null && layer.equalsIgnoreCase("3")) {
                                parentName += bb[4].toString();
                            } else if (layer != null && layer.equalsIgnoreCase("4")) {
                                parentName += bb[4].toString() + "," + bb[5].toString();
                            }
                            address.setParentName(parentName);
                            addresses.add(address);
                        }
                    }
                    results = new String[addresses.size()][2];
                    for (int i = 0; i < results.length; i++) {
                        results[i][0] = addresses.get(i).getName();
                        results[i][1] = addresses.get(i).getCode();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // TODO: 2016/8/13
        return null;
    }

    public String[][] getResults() {
        if (results == null || results.length == 0) {
            results = new String[1][2];
            results[0][0] = "未找到数据";
            results[0][1] = "\"\"";
        }
        return results;
    }
}
